package com.vitas.coin.ui.act

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.BarUtils
import com.snap.base.util.BaseUtil
import com.snap.common.main.BaseMVVMAct
import com.snap.util.edge.marginTopSystemWindowInsets
import com.snap.util.edge.paddingBottomSystemWindowInsets
import com.vitas.coin.R
import com.vitas.coin.data.event.AddSuccessEvent
import com.vitas.coin.data.event.ChooseEvent
import com.vitas.coin.data.event.RemoveEvent
import com.vitas.coin.data.event.UploadSuccessEvent
import com.vitas.coin.databinding.ActUploadBinding
import com.vitas.coin.vm.UploadVM


class UploadAct : BaseMVVMAct<ActUploadBinding, UploadVM>(){

    companion object {
        const val PARENT_ID = "PARENT_ID"
    }


    override fun createVM(): UploadVM {
        return UploadVM().apply {
            actionAct = { this@UploadAct }
        }
    }

    override fun doDataBind() {
        binding.viewModel = viewModel
        binding.page = this
    }

    override fun getContentViewId(): Int {
        return R.layout.act_upload
    }

    override fun onCreate() {
        binding.clContent.paddingBottomSystemWindowInsets()
        binding.rlTop.marginTopSystemWindowInsets()
        // BarUtils.addMarginTopEqualStatusBarHeight(binding.rlTop)
        BarUtils.transparentStatusBar(this)
        BarUtils.setStatusBarLightMode(this, true)

        initParentId()
        binding.tvUpload.isClickable = false
        initRv()
        initObserve()
    }

    private fun initParentId() {
        viewModel.initParentId(intent.getLongExtra(PARENT_ID, 0L))
    }


    private fun initObserve() {
        BaseUtil.observer(this, ChooseEvent::class.java) {
            viewModel.addMusic(it.musicBean)
        }

        BaseUtil.observer(this, AddSuccessEvent::class.java) {
            binding.llUploadSuccess.visibility = View.GONE
            binding.llList.visibility = View.VISIBLE
            binding.ivBagUpload.isClickable = false
            binding.tvUpload.setBackgroundResource(R.drawable.bg_upload)
            binding.tvUpload.isClickable = true
        }

        BaseUtil.observer(this, UploadSuccessEvent::class.java) {
            binding.llUploadSuccess.visibility = View.VISIBLE
            binding.llList.visibility = View.GONE
            binding.ivBagUpload.isClickable = true
            binding.tvUpload.setBackgroundResource(R.drawable.bg_upload_normal)
            binding.tvUpload.isClickable = false
        }

        BaseUtil.observer(this, RemoveEvent::class.java) {
            binding.llUploadSuccess.visibility = View.GONE
            binding.llList.visibility = View.GONE
            binding.ivBagUpload.isClickable = true
            binding.tvUpload.setBackgroundResource(R.drawable.bg_upload_normal)
            binding.tvUpload.isClickable = false
        }
    }

    private fun initRv() {
        binding.mRecyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        binding.mRecyclerView.adapter = viewModel.musicAdapter
    }



    fun onClickBack(view: View) {
        finish()
    }




}